package com.ilearn.zookeeper.conn;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.Before;
import org.junit.Test;

/**
 * @Project: seb-core
 * @Description:
 * @Version 1.0
 * @Throws SystemException:
 * @Author: <li>2018/12/18 lyndon Create 1.0
 * @Copyright (c)2018-2018 中央结算公司，版权所有
 * @Modified By:
 * <p>
 * PathChildCache 监听一个节点下子节点的创建、删除、更新
 * NodeCache 监听一个节点的更新和创建事件
 * TreeCache 综合PathChildCache和NodeCache的特性
 */
public class CuratorListener {

    private CuratorFramework curatorFramework;
    private String path;

    @Before
    public void init() {
        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("192.168.100.100:2181")
                .sessionTimeoutMs(4000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .namespace("curator")
                .build();

        path = "/huzy/node2";
        curatorFramework.start();
    }


    // 创建和修改节点的时候，才会有事件触发，删除节点或者创建修改删除子节点都不会有事件触发
    @Test
    public void addListenWithNodeCache() throws Exception {
        NodeCache nodeCache = new NodeCache(curatorFramework, path);

        final NodeCacheListener nodeCacheListener = new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("Receive node Event:" + nodeCache.getPath());
            }
        };

        nodeCache.getListenable().addListener(nodeCacheListener);

        nodeCache.start();

        System.in.read();
    }


    // 监听一个节点下子节点的创建、删除、更新
    @Test
    public void addListenWithPathChildCache() throws Exception {
        final PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, path, true);

        final PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                System.out.println("Received path Event:" + event.getType());
            }
        };

        pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);

        pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);

        System.in.read();
    }

    //
    @Test
    public void addListenWithTreeCache() throws Exception {
        final TreeCache treeCache = new TreeCache(curatorFramework, path);

        final TreeCacheListener treeCacheListener = new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                System.out.println("Received tree Event:" + event.getType() + " ==> " + event.getData().getPath());
            }
        };

        treeCache.getListenable().addListener(treeCacheListener);

        treeCache.start();

        System.in.read();

    }


}
